package com.carrot.newsgallery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carrot.newsgallery.entity.NewsChild;
import com.carrot.newsgallery.mapper.NewsChildMapper;
import com.carrot.newsgallery.service.NewsChildService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


@Service
public class NewsChildServiceImpl extends ServiceImpl<NewsChildMapper, NewsChild> implements NewsChildService {

    @Autowired
    private NewsChildMapper newsChildMapper;

    private static String[] hours = {" 04"," 10"," 16"," 22"};

    @Override
    public List<NewsChild> getToday(String tablename) {
        LambdaQueryWrapper<NewsChild> lqw = new LambdaQueryWrapper<>();
        //表名是在controller里动态设置的
        String date = LocalDate.now().toString();
        String yesterday = LocalDate.now().minusDays(1).toString();
        String hour = LocalDateTime.now().toString().substring(11,13);
        int hour_int = Integer.parseInt(hour);
        if (hour_int < 5)
            date = yesterday + hours[3];
        else if (hour_int < 11)
            date += hours[0];
        else if (hour_int < 17)
            date += hours[1];
        else if (hour_int < 23)
            date += hours[2];
        else
            date += hours[3];
        lqw.like(true,NewsChild::getCreateDate, date);
        List<NewsChild> list = newsChildMapper.selectList(lqw);
        for (NewsChild one : list){
            one.setHubIdentity(tablename);
        }
        return list;
    }

    @Override
    public List<NewsChild> getHistory(String date, int batch, String tablename) {
        //表名是在controller里动态设置的,这里不用管
        LambdaQueryWrapper<NewsChild> lqw = new LambdaQueryWrapper<>();
        date += hours[batch];
        lqw.like(true,NewsChild::getCreateDate,date);
        List<NewsChild> list = newsChildMapper.selectList(lqw);
        for (NewsChild one : list){
            one.setHubIdentity(tablename);
        }
        return list;
    }
}
